# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: Jerome Leclanche <jerome@leclan.ch>

pkgname=pgadmin4
pkgver=4.30
pkgrel=5
pkgdesc='Comprehensive design and management interface for PostgreSQL'
url='https://www.pgadmin.org/'
arch=('loong64' 'x86_64')
license=('custom')
depends=('qt5-base' 'postgresql-libs' 'hicolor-icon-theme' 'python'
         'libxcrypt' 'libcrypt.so' 'glibc' 'gcc-libs'
         'python-blinker' 'python-flask' 'python-flask-login'
         'python-flask-migrate' 'python-flask-sqlalchemy' 'python-flask-wtf'
         'python-passlib' 'python-pytz' 'python-simplejson' 'python-six'
         'python-speaklater' 'python-sqlparse' 'python-wtforms' 'python-psutil'
         'python-jinja' 'python-paramiko' 'python-psycopg2' 'python-bcrypt'
         'python-cryptography' 'python-sqlalchemy1.3' 'python-testtools'
         'python-webencodings' 'python-werkzeug' 'python-dateutil'
         'python-flask-gravatar' 'python-flask-mail' 'python-flask-principal'
         'python-flask-paranoid' 'python-sshtunnel' 'python-flask-security-too'
         'python-werkzeug' 'python-flask-compress' 'python-ldap3' 'python-cryptography'
         'python-flask-babelex' 'python-gssapi')
makedepends=('python-setuptools' 'python-sphinx' 'python-extras' 'python-fixtures'
             'python-html5lib' 'python-pbr' 'python-mimeparse' 'python-pyrsistent'
             'imagemagick')
source=(https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v${pkgver}/source/${pkgname}-${pkgver}.tar.gz{,.asc}
        pgAdmin4.desktop
        config_distro.py
        config_local.py
        pgadmin4-python-de-vendor-venv-paths.patch
        jinja-3.1.patch
        flask-security-compat.patch
        flask2-compat.patch)
validpgpkeys=('E8697E2EEF76C02D3A6332778881B2A8210976F2') # Package Manager (Package Signing Key) <packages@pgadmin.org>
sha512sums=('7ce12f65ce9dbfe4af9e92dd7f9327a05be8b8436ef495a0634043158417f3af5f457b88ee5422ede59f517c197668094454c6d7b7c33e255068479ce6ec13d9'
            'SKIP'
            'b19dda3331585010c759099eb09f4db288ce4cd3d36882b56748e1e3756dc7bee2899d7438d496280498ec6a60f6e1ba90309d49fc599403f1fdc7e8817b6645'
            '16d00dc2095904a6b12da7039458f632873829ad98d4d7653eac5804032ba92097ccae4488d56467d0ea9bd64e2654a3dead73eb7924c947ff1737ff6e3b4745'
            'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'
            'a8c1a3762469b02b745da4e3b30f7534ad6bb94e91d43f7e63604fef24f638e09575e2c9336e753e5b6dbe167c55bb49fb6247a16431415318479eb3a3712adc'
            'ec7b9ef6d944ac5b8e2c0e7ff7626af8950b369e34bb643322d9208908baea70ad6753ae9eff2799c04fe5182edbb5c270ee69647f224349f0df890a735a7767'
            '8b23686674c12f1bbba9ded22425874adb5f6802f2e1f7e381c07208251301f0bb56622a0440e5609c5dcc5461f50fe08c7dcab4405878e9662de6faf782a39b'
            '5eb39b2926adc7fe715a2b1b3f2165bd84a9fab3f8afda1ddaaba557ad1d41a4aa8d279387699b7148a86c7af4782d5b6b7f50f89972ef3507d14795c006a65c')

prepare() {
  cd ${pkgname}-${pkgver}

  patch -Np1 < ../pgadmin4-python-de-vendor-venv-paths.patch
  patch -Np1 --no-backup-if-mismatch < ../jinja-3.1.patch
  patch -NRp1 < ../flask-security-compat.patch
  patch -NRp1 < ../flask2-compat.patch

  local PYTHONVERSION="$(python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
  sed -E "s|/usr/pgadmin4/web|/usr/lib/pgadmin4/web|g;
          s|/usr/pgadmin4/lib/python[0-9\\.]+|/usr/lib/python${PYTHONVERSION}|g" \
      -i runtime/ConfigWindow.ui
  sed "s|##PYTHONVERSION##|${PYTHONVERSION}|g" -i runtime/Server.cpp

  sed -E -i requirements.txt \
    -e '/blinker>?=/d' \
    -e '/extras>?=/d' \
    -e '/Flask>?=/d' \
    -e '/Flask-Login>?=/d' \
    -e '/Flask-Migrate>?=/d' \
    -e '/Flask-SQLAlchemy>?=/d' \
    -e '/Flask-WTF>?=/d' \
    -e '/pycrypto>?=/d' \
    -e '/passlib>?=/d' \
    -e '/pytz>?=/d' \
    -e '/simplejson>?=/d' \
    -e '/six>?=/d' \
    -e '/speaklater>?=/d' \
    -e '/sqlparse>?=/d' \
    -e '/WTForms>?=/d' \
    -e '/psutil>?=/d' \
    -e '/psycopg2>?=/d' \
    -e '/python-dateutil>?=/d' \
    -e '/SQLAlchemy>?=/d' \
    -e '/Flask-Gravatar>?=/d' \
    -e '/Flask-Mail>?=/d' \
    -e '/Flask-Principal>?=/d' \
    -e '/Flask-Paranoid>?=/d' \
    -e '/htmlmin>?=/d' \
    -e '/Flask-Security-Too>?=/d' \
    -e '/Flask-HTMLmin>?=/d' \
    -e '/Flask-Compress>?=/d' \
    -e '/sshtunnel>?=/d' \
    -e '/Werkzeug>?=/d' \
    -e '/ldap3>?=/d' \
    -e '/bcrypt>?<?=/d' \
    -e '/cryptography>?<?=/d' \
    -e '/Flask-BabelEx>?<?=/d' \
    -e '/gssapi>?<?=/d' \
    -e '/^#.*/d' \
    -e '/^$/d'
  if [[ -s requirements.txt ]]; then
    echo "ERROR: requirements.txt must be empty:"
    cat requirements.txt
    exit 1
  fi
}

build() {
  export LANG=en_US.UTF-8
  export LC_ALL=en_US.UTF-8
  export PGADMIN_PYTHON_DIR=/usr

  cd ${pkgname}-${pkgver}
  convert +set date:create +set date:modify runtime/pgAdmin4.{ico,png}
  # override doctree directory
  # override SPHINXBUILD to not treat warnigs as errors
  make docs SPHINXOPTS='-d /tmp/' SPHINXBUILD=sphinx-build

  cd runtime
  export CFLAGS+=" ${CPPFLAGS}"
  export CXXFLAGS+=" ${CPPFLAGS}"
  qmake CONFIG+=release
  make
}

package() {
  cd ${pkgname}-${pkgver}

  install -Dm 755 runtime/pgAdmin4 "${pkgdir}/usr/lib/pgadmin4/runtime/pgAdmin4"
  cp -a docs web "${pkgdir}/usr/lib/pgadmin4"
  install -Dm 644 "${srcdir}"/config_{distro,local}.py -t "${pkgdir}/usr/lib/pgadmin4/web"

  install -Dm 644 runtime/pgAdmin4-0.png "${pkgdir}/usr/share/icons/hicolor/256x256/apps/pgAdmin4.png"
  install -Dm 644 runtime/pgAdmin4-1.png "${pkgdir}/usr/share/icons/hicolor/48x48/apps/pgAdmin4.png"
  install -Dm 644 runtime/pgAdmin4-2.png "${pkgdir}/usr/share/icons/hicolor/32x32/apps/pgAdmin4.png"
  install -Dm 644 runtime/pgAdmin4-3.png "${pkgdir}/usr/share/icons/hicolor/16x16/apps/pgAdmin4.png"
  install -Dm 644 "${srcdir}/pgAdmin4.desktop" -t "${pkgdir}/usr/share/applications"

  install -D /dev/stdin "${pkgdir}/usr/bin/pgadmin4" <<END
#!/bin/sh
cd /usr/lib/pgadmin4
exec runtime/pgAdmin4 "\$@"
END
  install -D /dev/stdin "${pkgdir}/usr/bin/pgadmin4-server" <<END
#!/bin/sh
cd /usr/lib/pgadmin4
python web/pgAdmin4.py "\$@"
END

  install -Dm 644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

# vim: ts=2 sw=2 et:
